package com.ftf.tp.blog.infrastructure.config;

import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch._types.aggregations.Aggregation;
import co.elastic.clients.elasticsearch._types.aggregations.TermsAggregation;
import lombok.Getter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tanghao
 * @date 2022/8/22
 */
@Getter
public class EsAggWrapper {

    private Aggregation.Builder aggregation;

    private TermsAggregation.Builder termsAggregation;

    private List<Map<String, SortOrder>> orderList;

    private List<EsAggWrapper> esAggWrapperList;

    private String aggName;

    /**
     * 聚合层级
     */
    private Integer level;

    public EsAggWrapper() {
    }


    protected void init(String aggName, Integer level) {
        aggregation = new Aggregation.Builder();
        orderList = new ArrayList<>();
        this.aggName = aggName;
        this.level = level;
        esAggWrapperList = new ArrayList<>();
    }

    /**
     * 提供给外层调用的
     */
    protected EsAggWrapper aggTermsInit(String aggName, String field, Integer level) {
        init(aggName, level);
        termsAggregation = new TermsAggregation.Builder().field(field);
        return this;
    }


    public EsAggWrapper size(Integer value) {
        termsAggregation.size(value);
        return this;
    }

    public EsAggWrapper sort(String field, SortOrder sortOrder) {
        Map<String, SortOrder> sortOrderMap = new HashMap<>();
        sortOrderMap.put(field, sortOrder);
        orderList.add(sortOrderMap);
        return this;
    }

    public void aggChild(String aggName, String field) {

    }


}
